package at.tuwien.seminar.scrum1.hibernate;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class HibernateUtil {
	
	private static EntityManagerFactory entityManagerFactory = null;

	static {
		try {
			 entityManagerFactory = Persistence.createEntityManagerFactory("app");
  		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
            ex.printStackTrace();
			System.err.println("Initial EntityManagerFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static EntityManagerFactory getTestEntityManagerFactory() {
		try {
			 return Persistence.createEntityManagerFactory("test");
  		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial EntityManagerFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}


	public static EntityManagerFactory getEntityManagerFactory() {
		return entityManagerFactory;
	}
	
	public static void shutdown() {
		//Close caches and connection pools
		getEntityManagerFactory().close();
	}
}
